package com.ki.rent.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ki.rent.entity.Host;
import com.ki.rent.entity.House;
import com.ki.rent.entity.User;
import com.ki.rent.service.HostService;
import com.ki.rent.service.HouseService;
import com.ki.rent.service.UserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author
 */
@Controller
@RequestMapping("/rent/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private HostService hostService;
    @Autowired
    private HouseService houseService;

    @RequestMapping("/login")
    public String login(@RequestParam("account") String account,
                        @RequestParam("password") String password,
                        Model model, HttpSession session) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("account", account);
        User user = userService.getOne(wrapper);
        if (user == null) {
            model.addAttribute("msg", "账号或密码错误");
            return "index";
        }
        List<House> houses = houseService.list(null);
        model.addAttribute("houses", houses);
        if ((user.getPassword()).equals(password)) {
            session.setAttribute("loginUser", user);
            session.setAttribute("userId", user.getUserId());
            model.addAttribute("user", user);
            return "house/allhouse";
        }
        model.addAttribute("msg", "账号或密码错误");
        return "index";
    }

    //注册用户
    @RequestMapping("/addUserRecord")
    @ResponseBody
    public String addUser(User user, HttpSession session) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("account", user.getAccount());
        System.out.println(user);
        User one = userService.getOne(wrapper);
        if (one == null) {
            boolean n = userService.save(user);
            if (n) {
                session.setAttribute("loginUser", user);
                session.setAttribute("userId", user.getUserId());
                session.setAttribute("user", user);
                return "OK";
            }
        }
        return "FAIL";
    }

    //用户注册完跳到主页
    @RequestMapping("/toIndex")
    public String toIndex(HttpSession session, Model model) {
        User user = (User) session.getAttribute("user");
        model.addAttribute("user", user);
        return "html/userIndex";
    }

    //用户到更新页面
    @RequestMapping("/toUpdate")
    public String toUpdate(Model model, HttpSession session) {
        Integer userId = (Integer) session.getAttribute("userId");
        User user = userService.getById(userId);
        model.addAttribute("user", user);
        return "html/userUpdate";
    }

    //用户注册
    @RequestMapping("/addUser")
    public String addUser(User user, Model model) {
        userService.save(user);
        model.addAttribute("msg", "注册成功，请登录");
        return "index";
    }

    //更新完成
    @RequestMapping("/update")
    public String update(User user, Model model) {
        userService.updateById(user);
        model.addAttribute("user", user);
        model.addAttribute("msg", "信息修改成功");
        return "html/userUpdate";
    }

    //用户注销
    @RequestMapping("/signout")
    public String logout(HttpSession session) {
        session.invalidate();
        return "redirect:/styleindex.html";
    }

    //    前往注册房东
    @RequestMapping("/sign/{userId}")
    public String sign(@PathVariable("userId") Integer userId, Model model) {
        model.addAttribute("userId", userId);
        if (hostService.getById(userId) != null) {
            Host host = hostService.getById(userId);
            model.addAttribute("host", host);
            return "html/xiugai";
        }
        return "html/hostSign";
    }

    //到主页面
    @RequestMapping("/main")
    public String main(Model model, HttpServletRequest request) {
        Integer hostId = (Integer) request.getSession().getAttribute("userId");
        User user = userService.getById(hostId);
        model.addAttribute("user", user);
        if (hostService.getById(hostId) == null) {
            return "html/userIndex";
        }
        return "html/customer";
    }

    //注册房东成功
    @RequestMapping("/toMain")
    public String toMain(Host host, Model model, HttpServletRequest request) {
        Integer userId = (Integer) request.getSession().getAttribute("userId");
        User user = userService.getById(userId);
        host.setHostId(user.getUserId());
        hostService.saveOrUpdate(host);
        model.addAttribute("host", host);
        model.addAttribute("msg", "成功注册成为房东,重新登录以进入房东页面");
        model.addAttribute("user", user);
        return "html/hostSign";
    }

    //查看我的主页
    @RequestMapping("/myself/{userId}")
    public String myself(@PathVariable("userId") Integer userId, Model model) {
        User user = userService.getById(userId);
        model.addAttribute("user", user);
        return "html/myself";
    }

    //去添加用户页面
    @RequestMapping("/toAddUser")
    public String toAddUser() {
        return "html/index";
    }

    //注销页面
    @RequestMapping("/Logout")
    public String Logout(Model model, HttpServletRequest request) {
        Integer userId = (Integer) request.getSession().getAttribute("userId");
        boolean b = userService.removeById(userId);
        User user = userService.getById(userId);
        model.addAttribute("user", user);
        if (b) {
            return "index";
        } else {
            return "html/customer";
        }
    }

    //去更新用户密码页面
    @RequestMapping("/toUpdatePwd")
    public String toUpdatePwd(HttpServletRequest request) {
        return "html/updatepwd";
    }

    //更新密码
    @ResponseBody
    @RequestMapping("/updatePwd")
    public String updatePwd(String oldpwd, String password, HttpServletRequest request) {
        Integer userId = (Integer) request.getSession().getAttribute("userId");
        User user = userService.getById(userId);
        if (!user.getPassword().equals(oldpwd)) {
            return "FAIL";
        }
        user.setPassword(password);
        boolean b = userService.saveOrUpdate(user);
        if (b) {
            return "OK";
        } else {
            return "FAIL";
        }
    }

    //查看我的主页
    @RequestMapping("/myself")
    public String myself(Model model, HttpServletRequest request) {
        Integer userId = (Integer) request.getSession().getAttribute("userId");
        User user = userService.getById(userId);
        model.addAttribute("user", user);
        return "html/myself";
    }

    @RequestMapping("/toUserIndex")
    public String toUserIndex(HttpSession session, Model model) {
        User user = (User) session.getAttribute("loginUser");
        if (user == null){
            return "redirect:/index";
        }
        model.addAttribute("user", user);
        Integer hostId = user.getUserId();
        if (hostService.getById(hostId) == null) {
            return "html/userIndex";
        }
        return "html/customer";
    }
}

